package cc.linker.steplink.crm.merchanth5.controller;

import cc.linker.steplink.crm.commons.Constants;
import cc.linker.steplink.crm.commons.coupon.CouponCustomerInfo;
import cc.linker.steplink.crm.commons.domain.WxUser;
import cc.linker.steplink.crm.commons.model.SystemProperties;
import cc.linker.steplink.crm.commons.service.CommonsService;
import cc.linker.steplink.crm.commons.utils.TransactionOrderUtil;
import cc.linker.steplink.crm.merchanth5.commons.RequestFilter;
import cc.linker.steplink.crm.merchanth5.model.OrderAddressDto;
import cc.linker.steplink.crm.merchanth5.pojo.MerchantOrderPo;
import cc.linker.steplink.crm.merchanth5.pojo.WxpayReturnInfo;
import cc.linker.steplink.crm.merchanth5.service.MallService;
import cc.linker.steplink.crm.merchanth5.util.ParamSignUtils;
import cc.linker.steplink.crm.response.GlobalException;
import cc.linker.steplink.crm.response.ResponseCode;
import cc.linker.steplink.crm.response.ResponseException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.mongodb.util.JSON;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 商城
 */
@RestController
@RequestMapping("/mall")
public class MallController {

    @Autowired
    private MallService mallService;
    @Autowired
    private SystemProperties systemProperties;

    @Autowired
    private RequestFilter requestFilter;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private CommonsService commonsService;
    @Value("${yzj.orgId}")
    public String yzjOrgId;

    /**
     * 商品列表
     *
     * @param orgId     机构id
     * @param pageIndex 页码
     * @param pageSize  页面大小
     * @param status    状态
     * @param key       关键字
     * @return
     */
    @PostMapping("/getGoodsList")
    public Object getGoodsList(@RequestBody Map<String, Object> params, HttpServletRequest request) {
        WxUser wxUser =  requestFilter.getWxUser(request);
        String orgId = wxUser.getOrgId();
        params.put("orgId", orgId);
        return mallService.getGoodsList(params);
    }

    /**
     * 获取商品详情
     *
     * @param goodsId
     * @param merchantId
     * @return
     */
    @PostMapping("/getGoodsInfo")
    public Object getGoodsInfo(@RequestBody Map<String, Object> params, HttpServletRequest request) {
        WxUser wxUser = requestFilter.getWxUserNotLogin(request);
        if (wxUser != null) {
            params.put("orgId", wxUser.getOrgId());
            params.put("customerId", wxUser.getCustomerId());
        }
        return mallService.getGoodsInfo(params);
    }

    /**
     * 获取商品sku
     *
     * @param goodsId
     * @param merchantId
     * @param wxappletId
     * @return
     */
    @PostMapping("/getGoodsSku")
    public Object getGoodsSku(@RequestBody Map<String, Object> params) {
        return mallService.getGoodsSku(params);
    }

    /**
     * 获取商品规格
     *
     * @param goodsId
     * @param merchantId
     * @param wxappletId
     * @return
     */
    @PostMapping("/getGoodsSpec")
    public Object getGoodsSpec(@RequestBody Map<String, Object> params) {
        return mallService.getGoodsSpec(params);
    }

    /**
     * 加载购物车
     *
     * @param userId
     * @return
     */
    @PostMapping("/getShopingCart")
    public Object getShoppingCart(HttpServletRequest request) {
        WxUser wxUser =  requestFilter.getWxUser(request);
        String customerId = wxUser.getCustomerId();
        Map<String, Object> params = new HashMap<>();
        params.put("customerId", customerId);
        params.put("isVip", wxUser.getIsVip());
//        return mallService.getShoppingCart(params);
        return mallService.getShoppingCartNew(params);
    }

    /**
     * 加入购物车
     *
     * @param buyCount
     * @param goodsId
     * @param merchantId /mall/getShopingCart
     * @param skuId
     * @returngetShopingCart
     */
    @PostMapping("/addShoppingCart")
    public void addShoppingCart(@RequestBody Map<String, Object> params, HttpServletRequest request) {
        WxUser wxUser =  requestFilter.getWxUser(request);
        String customerId = wxUser.getCustomerId();
        params.put("customerId", customerId);
        mallService.addShoppingCart(params);
    }

    /**
     * 编辑购物车
     *
     * @param userId
     * @param buyCount
     * @param shoppingCartId
     * @return
     */
    @PostMapping("/editShoppingCart")
    public Object editShoppingCart(@RequestBody Map<String, Object> params, HttpServletRequest request) {
        WxUser wxUser =  requestFilter.getWxUser(request);
        String customerId = wxUser.getCustomerId();
        params.put("customerId", customerId);
        return mallService.editShoppingCart(params);
    }

    /**
     * 删除购物车
     *
     * @param userId
     * @param shoppingCartIds
     */
    @PostMapping("/delShoppingCart")
    public void delShoppingCart(@RequestBody Map<String, Object> params, HttpServletRequest request) {
        String shoppingCartIds = (String) params.get("shoppingCartIds");
        if (shoppingCartIds != null) {
            String[] shoppingCartIdList = shoppingCartIds.split(",");
            List<String> cartIdList = Arrays.asList(shoppingCartIdList);
            params.put("shoppingCartIds", cartIdList);
        }
        WxUser wxUser =  requestFilter.getWxUser(request);
        String customerId = wxUser.getCustomerId();
        params.put("customerId", customerId);
        mallService.delShoppingCart(params);
    }

    /**
     * 清空购物车
     *
     */
    @PostMapping("/delAllShoppingCart")
    public void delAllShoppingCart(HttpServletRequest request) {
        Map params = new HashMap();
        WxUser wxUser =  requestFilter.getWxUser(request);
        String customerId = wxUser.getCustomerId();
        params.put("customerId", customerId);
        mallService.delAllShoppingCart(params);
    }

    /**
     * 确认订单
     *
     * @param shoppingCartIds
     * @param goodsId
     * @param skuId
     * @param buyCount
     * @return
     */
    @PostMapping("/checkShoppingOrder")
    public Object checkShoppingOrder(@RequestBody Map<String, Object> params, HttpServletRequest request) {
        String shoppingCartIds = (String) params.get("shoppingCartIds");
        if (shoppingCartIds != null) {
            String[] shoppingCartIdList = shoppingCartIds.split(",");
            List<String> cartIdList = Arrays.asList(shoppingCartIdList);
            params.put("shoppingCartIds", cartIdList);
        }
        WxUser wxUser =  requestFilter.getWxUser(request);
        String customerId = wxUser.getCustomerId();
        params.put("customerId", customerId);
        return mallService.checkShoppingOrder(params);
    }

    /**
     * 获取默认收货地址
     *
     * @param addressId
     * @param userId
     * @return
     */
    @PostMapping("/getDefaultReceiveAddress")
    public OrderAddressDto getDefaultReceiveAddress(@RequestBody Map<String, Object> params, HttpServletRequest request) {
//        Map<String, Object> params = new HashMap<>();
        WxUser wxUser =  requestFilter.getWxUser(request);
        String customerId = wxUser.getCustomerId();
        params.put("customerId", customerId);
        return mallService.getDefaultReceiveAddress(params);
    }

    /**
     * 获取收货地址列表
     *
     * @param userId
     * @return
     */
    @PostMapping("/getReceiveAddressList")
    public Object getReceiveAddressList(HttpServletRequest request) {
        Map<String, Object> params = new HashMap<>();
        WxUser wxUser =  requestFilter.getWxUser(request);
        String customerId = wxUser.getCustomerId();
        params.put("customerId", customerId);
        return mallService.getReceiveAddressList(params);
    }

    /**
     * 保存收货地址
     *
     * @param addressId
     * @param addressInfo
     * @param areaName
     * @param cityName
     * @param isDefault
     * @param phone
     * @param provinceName
     * @param realName
     * @param userId
     */
    @PostMapping("/saveReceiveAddress")
    public void saveReceiveAddress(@RequestBody Map<String, Object> params, HttpServletRequest request) {
        WxUser wxUser =  requestFilter.getWxUser(request);
        String customerId = wxUser.getCustomerId();
        params.put("customerId", customerId);
        mallService.saveReceiveAddress(params);
    }

    /**
     * 删除收货地址
     *
     * @param addressId
     * @param userId
     */
    @PostMapping("/delReceiveAddress")
    public void delReceiveAddress(@RequestBody Map<String, Object> params, HttpServletRequest request) {
        WxUser wxUser =  requestFilter.getWxUser(request);
        String customerId = wxUser.getCustomerId();
        params.put("customerId", customerId);
        mallService.delReceiveAddress(params);
    }

    /**
     * 购买商品
     *
     * @param request
     * @param addressId
     * @param goodsId
     * @param goodsNum
     * @param payType
     * @param skuId
     * @param userId
     * @param shoppingCartIds
     * @param merchantId
     * @param orderSn
     * @param wxappletId
     * @param distributor     分销人id
     * @return
     */
    @PostMapping("/createPayOrderNo")
    public Object createGoodsPayOrderNo(@RequestBody Map<String, String> params, HttpServletRequest request) throws GlobalException {
        WxUser wxUser =  requestFilter.getWxUser(request);
        String customerId = wxUser.getCustomerId();
        String openId = wxUser.getOpenid();
//        String openId = "oRXb40VQIiVcfe50ECwl-_ZMNKhs";
        String orgId = wxUser.getOrgId();
        Integer payType1 = 2;
        String notifyUrl = systemProperties.getScrmUrl() + "mall/orderNotify";

        String addressId = params.get("addressId");
        Long addressId1 = null;
        if (!StringUtils.isEmpty(addressId)) {
            addressId1 = Long.valueOf(addressId);
        }
        String goodsId = params.get("goodsId");
        Long goodsId1 = null;
        if (!StringUtils.isEmpty(goodsId)) {
            goodsId1 = Long.valueOf(goodsId);
        }
        String skuId = params.get("skuId");
        Long skuId1 = null;
        if (!StringUtils.isEmpty(skuId)) {
            skuId1 = Long.valueOf(skuId);
        }
        String goodsNum = params.get("goodsNum");
        //修改商品默认个数为1
        Integer goodsNum1 = 1;
        if (!StringUtils.isEmpty(goodsNum)) {
            goodsNum1 = Integer.parseInt(goodsNum);
            goodsNum1 = goodsNum1 == 0 ? 1 : goodsNum1;
        }
        String orderSn1 = StringUtil.isEmpty(params.get("orderSn")) ? null : params.get("orderSn");
        String shoppingCartIds = StringUtil.isEmpty(params.get("shoppingCartIds")) ? null : params.get("shoppingCartIds");
        List<String> cartIdList = null;
        if (shoppingCartIds != null) {
            String[] shoppingCartIdList = shoppingCartIds.split(",");
            cartIdList = Arrays.asList(shoppingCartIdList);
        }
//        String payType = "APPLET_WXJZ";
        String payType = "STEPLINK_" + orgId;
        String distributor = null;
        if (params.containsKey("distributor")) {
            distributor = params.get("distributor");
        }
//        String distributorT = null;
//        if (params.containsKey("distributorT")) {
//            distributorT = params.get("distributorT");
//        }
        return mallService.createGoodsPayOrderNo(request, addressId1, goodsId1, goodsNum1,
                payType1, skuId1, customerId, cartIdList, orgId, orderSn1, openId, payType, notifyUrl, distributor);
    }

    /**
     * 商品订单回调
     */
    @PostMapping("/orderNotify")
    public void orderNotify(@RequestBody Map<String, Object> params) {
        mallService.orderNotifyShare(params);
    }

    /**
     * 提交订单
     *
     * @param request
     * @return
     */
//    @PostMapping("/submitShoppingOrder")
//    public String submitShoppingOrder(Long wxpayTradeId,Integer payStatus,HttpServletRequest request){
//        Map<String, Object> paraMap = new HashMap<>();
//        paraMap.put("wxpayTradeId", wxpayTradeId);
//        paraMap.put("payStatus", payStatus);
//        return mallService.submitShoppingOrder(paraMap,request);
//    }

    @Value("${wcpay.payurl}")
    private String payurl;

    //    @PostMapping("/createOrder")
    public Object createOrder(@RequestBody Map<String, Object> params, HttpServletRequest request) throws Exception {
        WxUser wxUser =  requestFilter.getWxUser(request);
        SimpleDateFormat utime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //订单参数
        String customerId = wxUser.getCustomerId();//用户id
        String nickName = wxUser.getNickname();//昵称
        String orgId = wxUser.getOrgId();//机构id
        String orderSn = TransactionOrderUtil.TYPE_SHOP + TransactionOrderUtil.getPrimaryKey();//订单号
        String createTime = utime.format(new Date());//创建时间
        String orderFrom = "4";//订单来源

        Map<String, Object> addrParam = new HashMap<>();
        addrParam.put("addressId", params.get("addressId"));
        addrParam.put("customerId", customerId);
        HashMap<String, Object> userAddr = mallService.getUserAddr(addrParam);
        String realName = userAddr.get("realName").toString();//收货人
        String phone = userAddr.get("phone").toString();//手机号
        String province = userAddr.get("province").toString();//收货人省份
        String city = userAddr.get("city").toString();//收货人市
        String area = userAddr.get("area").toString();//收货人区
        String detail = userAddr.get("detail").toString();//收货人详细地址

        String skuParam = params.get("skuParam").toString();
//        String skuParam = "[{\"skuId\":1,\"skuNum\":20},{\"skuId\":2,\"skuNum\":10}]";
        List skuList = (List) JSON.parse(skuParam);

        List<Map<String, Object>> orderGoodsTmp = new ArrayList<>();
        int goodsAmount = 0;//商品总价格(单位：分)
        int orderAmount = 0;//订单应付金额(单位：分)
        int orderTotalPrice = 0;//订单总价格(单位：分)
        for (Object sku : skuList) {
            if (sku instanceof Map) {
                Integer skuId = (Integer) ((Map) sku).get("skuId");
                Integer skuNum = (Integer) ((Map) sku).get("skuNum");
                HashMap<String, Object> skuInfo = mallService.getSkuInfo(skuId);
                Integer goodsCount = Integer.parseInt(skuInfo.get("goodsCount").toString());//库存
                Integer moneyAmount = Integer.parseInt(skuInfo.get("moneyAmount").toString());//单价
                if (goodsCount < 1 || skuNum > goodsCount) {
                    throw new GlobalException(new ResponseException(400, skuInfo.get("skuValue").toString() + "库存不足"));
                }
                goodsAmount = goodsAmount + moneyAmount * skuNum;
                orderAmount = orderAmount + moneyAmount * skuNum;
                orderTotalPrice = orderTotalPrice + moneyAmount * skuNum;

                Map<String, Object> ogTmp = new HashMap<>();
                Integer goodsId = Integer.parseInt(skuInfo.get("goodsId").toString());//商品id
                ogTmp.put("goodsId", goodsId);
                ogTmp.put("skuId", skuId);
                ogTmp.put("skuKey", skuInfo.get("skuKey").toString());
                ogTmp.put("skuInfo", skuInfo.get("skuValue").toString());
                ogTmp.put("goodsPrice", moneyAmount);
                ogTmp.put("goodsNum", skuNum);
                orderGoodsTmp.add(ogTmp);
            }
        }

        Map<String, Object> orderParam = new HashMap<>();
        orderParam.put("customerId", customerId);
        orderParam.put("nickName", nickName);
        orderParam.put("orgId", orgId);
        orderParam.put("orderSn", orderSn);
        orderParam.put("createTime", createTime);
        orderParam.put("orderFrom", orderFrom);
        orderParam.put("realName", realName);
        orderParam.put("phone", phone);
        orderParam.put("province", province);
        orderParam.put("city", city);
        orderParam.put("area", area);
        orderParam.put("detail", detail);
        orderParam.put("goodsAmount", goodsAmount);
        orderParam.put("orderAmount", orderAmount);
        orderParam.put("orderTotalPrice", orderTotalPrice);

        Long orderId = mallService.addOrder(orderParam);

        List<Map<String, Object>> orderGoodslist = new ArrayList<>();
        for (Object og : orderGoodsTmp) {
            Integer goodsId = Integer.parseInt(((Map) og).get("goodsId").toString());
            HashMap<String, Object> goodsInfo = mallService.getGoodsInfo(goodsId);
            Map<String, Object> orderGoodsParam = new HashMap<>();
            orderGoodsParam.put("orderId", orderId);
            orderGoodsParam.put("goodsId", ((Map) og).get("goodsId"));
            orderGoodsParam.put("goodsName", goodsInfo.get("goodsName"));
            orderGoodsParam.put("skuId", ((Map) og).get("skuId"));
            orderGoodsParam.put("skuKey", ((Map) og).get("skuKey"));
            orderGoodsParam.put("skuInfo", ((Map) og).get("skuInfo"));
            orderGoodsParam.put("goodsPrice", ((Map) og).get("goodsPrice"));
            orderGoodsParam.put("goodsNum", ((Map) og).get("goodsNum"));
            orderGoodsParam.put("goodsImage", goodsInfo.get("goodsImage"));
            orderGoodsParam.put("createTime", createTime);
            orderGoodsParam.put("shippingFee", goodsInfo.get("goodsFreight"));
            orderGoodsParam.put("customerId", customerId);
            orderGoodslist.add(orderGoodsParam);
        }
        Integer orderGoodsCount = mallService.addOrderGoods(orderGoodslist);
        //订单创建完成


        Long currentTime = new Date().getTime();
        String notifyUrl = systemProperties.getScrmUrl() + "mall/orderNotify";
        Map<String, String> oparams = new HashMap<>();
        oparams.put("body", "STEP商城订单");
        oparams.put("notifyUrl", notifyUrl);
        oparams.put("openid", wxUser.getOpenid());
        oparams.put("outTradeNo", orderSn);
        oparams.put("timestamp", currentTime.toString());
        oparams.put("totalFee", String.valueOf(goodsAmount));
        oparams.put("tradeType", "JSAPI");
        oparams.put("wcpayType", "STEPLINK_" + orgId);
        Map<String, String> appSign = ParamSignUtils.sign(oparams);
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("appSign", appSign.get("appSign")));
        nvps.add(new BasicNameValuePair("notifyUrl", oparams.get("notifyUrl")));
        nvps.add(new BasicNameValuePair("body", oparams.get("body")));
        nvps.add(new BasicNameValuePair("outTradeNo", oparams.get("outTradeNo")));
        nvps.add(new BasicNameValuePair("openid", oparams.get("openid")));
        nvps.add(new BasicNameValuePair("timestamp", oparams.get("timestamp")));
        nvps.add(new BasicNameValuePair("totalFee", oparams.get("totalFee")));
        nvps.add(new BasicNameValuePair("tradeType", oparams.get("tradeType")));
        nvps.add(new BasicNameValuePair("wcpayType", oparams.get("wcpayType")));
        String result = ParamSignUtils.callService(payurl + "/pay/wcpay", nvps, 0, 0);
        Object parse = JSON.parse(result);
        Map ret = (Map) parse;
        String object = (String) ret.get("object");
        WxpayReturnInfo wxpayReturnInfo = (WxpayReturnInfo) JSON.parse(object);
        SortedMap<String, Object> finalpackage = new TreeMap<String, Object>();
        if ((Integer) ret.get("rt") == 1) {
            finalpackage.put("appId", wxpayReturnInfo.getAppId());
            finalpackage.put("timeStamp", wxpayReturnInfo.getTimeStamp());
            finalpackage.put("nonceStr", wxpayReturnInfo.getNonceStr());
            finalpackage.put("package", "prepay_id=" + wxpayReturnInfo.getPrepayId());
            finalpackage.put("signType", wxpayReturnInfo.getSignType());
            finalpackage.put("paySign", wxpayReturnInfo.getPaySign());
            finalpackage.put("orderSn", orderSn);
            return finalpackage;
        }
        return "";
//        return mallService.addOrderGoods(orderGoodslist);
//        return mallService.addOrder(orderParam);
    }


    /**
     * 获取订单列表
     *
     * @param userId
     * @param pageIndex
     * @return
     */
    @PostMapping("/getOrderList")
    public Object getOrderList(@RequestBody Map<String, Object> params, HttpServletRequest request) {
        WxUser wxUser =  requestFilter.getWxUser(request);
        String customerId = wxUser.getCustomerId();
        params.put("customerId", customerId);
        String orgId = wxUser.getOrgId();
        params.put("orgId", orgId);
        return mallService.getOrderList(params);
    }

    /**
     * 获取订单详情
     *
     * @param userId
     * @param orderId
     * @return
     */
    @PostMapping("/getOrderInfo")
    public Object getOrderInfo(@RequestBody Map<String, Object> params, HttpServletRequest request) throws GlobalException, ParseException {
        if(StringUtils.isEmpty(params.get("orderId")) && StringUtils.isEmpty(params.get("orderSn"))){
            throw GlobalException.build("参数错误");
        }
        WxUser wxUser =  requestFilter.getWxUser(request);
        String orgId = wxUser.getOrgId();
        params.put("orgId", orgId);
        return mallService.getOrderInfo(params);
    }

    /**
     * 删除订单
     *
     * @param userId
     * @param orderId
     */
    @PostMapping("/delShoppingOrder")
    public void delShoppingOrder(@RequestBody Map<String, Object> params, HttpServletRequest request) {
        WxUser wxUser =  requestFilter.getWxUser(request);
        String customerId = wxUser.getCustomerId();
        params.put("customerId", customerId);
        String orgId = wxUser.getOrgId();
        params.put("orgId", orgId);
        mallService.delShoppingOrder(params);
    }
    /**
     * 删除订单
     *
     * @param userId
     * @param orderId
     */
    @PostMapping("/delIntegralOrder")
    public void delIntegralShoppingOrder(@RequestBody Map<String, Object> params, HttpServletRequest request) {
        WxUser wxUser =  requestFilter.getWxUser(request);
        String customerId = wxUser.getCustomerId();
        params.put("customerId", customerId);
        String orgId = wxUser.getOrgId();
        params.put("orgId", orgId);
        mallService.delIntegralShoppingOrder(params);
    }

    /**
     * 取消订单
     *
     * @param userId
     * @param orderId
     */
    @PostMapping("/cancelShoppingOrder")
    public void cancelShoppingOrder(@RequestBody Map<String, Object> params, HttpServletRequest request) throws GlobalException {
        WxUser wxUser =  requestFilter.getWxUser(request);
        String customerId = wxUser.getCustomerId();
        params.put("customerId", customerId);
        String orgId = wxUser.getOrgId();
        params.put("orgId", orgId);
        CouponCustomerInfo couponCustomerInfo = requestFilter.getCouponCustomerInfo(request);
        params.put("couponCustomerInfo", couponCustomerInfo);
        mallService.cancelShoppingOrder(params);
    }

    /**
     * 确认收货
     *
     * @param userId
     * @param orderId
     */
    @PostMapping("/checkReceiveOrder")
    public void checkReceiveOrder(@RequestBody Map<String, Object> params, HttpServletRequest request) throws GlobalException {
        WxUser wxUser =  requestFilter.getWxUser(request);
        String customerId = wxUser.getCustomerId();
        params.put("customerId", customerId);
        params.put("orgId", wxUser.getOrgId());
        mallService.checkReceiveOrder(params);
    }

    /**
     * 获取订单中的商品
     * @param orderSn
     * @param request
     * @return
     */
    @GetMapping("/getordergoods")
    public List<Map<String,Object>> getOrderGoods (@RequestParam(value = "orderSn", required = true) String orderSn, HttpServletRequest request){
        WxUser wxUser =  requestFilter.getWxUser(request);
        String customerId = wxUser.getCustomerId();
        Map<String, Object> params = new HashMap<>();
        params.put("customerId", customerId);
        params.put("orgId", wxUser.getOrgId());
        params.put("orderSn", orderSn);
        return mallService.getOrderGoods(params);
    }


    /**
     * 申请退款-获取退款选项
     * @param goodsStatus
     * @return
     */
    @GetMapping("/getrefundway")
    public List<Map<String, Object>> getrefundway(@RequestParam(value = "goodsStatus", required = true) Integer goodsStatus) {

        List<Map<String, Object>> refundWay = new ArrayList<>();
        if (goodsStatus == 1) {//未发货
            Map<String, Object> way = new HashMap<>();
            way.put("refundWay", 0);
            way.put("title", "仅退款");
            way.put("desc", "无需退货仅需退款");
            refundWay.add(way);
        } else if (goodsStatus == 2) {//已发货
            Map<String, Object> way = new HashMap<>();
            way.put("refundWay", 0);
            way.put("title", "仅退款");
            way.put("desc", "无需退货仅需退款");
            refundWay.add(way);
            Map<String, Object> way1 = new HashMap<>();
            way1.put("refundWay", 1);
            way1.put("title", "退货退款");
            way1.put("desc", "已收到货，需要退货退款");
            refundWay.add(way1);
        } else {
            throw GlobalException.build("参数错误");
        }
        return refundWay;

    }


    /**
     * 申请退款-获取信息
     * @param orderSn
     * @param recIds
     * @param request
     * @return
     */
    @GetMapping("/getrefundamount")
    public Map<String,Object> getRefundAmount(@RequestParam(value = "orderSn") String orderSn,
                                              @RequestParam(value = "recIds") String recIds,
                                              HttpServletRequest request){
        if(StringUtils.isEmpty(orderSn) || StringUtils.isEmpty(recIds)){
            throw GlobalException.build("参数错误");
        }
        Map<String, Object> param = new HashMap<>();
        WxUser wxUser =  requestFilter.getWxUser(request);
        String customerId = wxUser.getCustomerId();
        param.put("customerId", customerId);
        param.put("orgId", wxUser.getOrgId());
        param.put("orderSn",orderSn);
        param.put("recIds",recIds);
        return mallService.getRefundAmount(param);
    }

    /**
     * 申请退款操作
     * @param request
     * @param params
     */
    @PostMapping("/applyrefund")
    public void applyRefund(HttpServletRequest request,@RequestBody Map<String, Object> params){
        if(StringUtils.isEmpty(params.get("recIds")) || StringUtils.isEmpty(params.get("orderSn")) || StringUtils.isEmpty(params.get("refundPhone")) || StringUtils.isEmpty(params.get("refundWay"))){
            throw GlobalException.build("参数错误");
        }
        WxUser wxUser =  requestFilter.getWxUser(request);
        String customerId = wxUser.getCustomerId();
        params.put("customerId", customerId);
        params.put("orgId", wxUser.getOrgId());
        mallService.applyRefund(params);
    }

    @GetMapping("/getordergoodsinfo")
    public List<Map<String, Object>> getOrderGoodsInfo(@RequestParam(value = "orderSn") String orderSn,HttpServletRequest request){
        if(StringUtils.isEmpty(orderSn)){
            throw GlobalException.build("参数错误");
        }
        Map<String,Object> param = new HashMap<>();
        WxUser wxUser =  requestFilter.getWxUser(request);
        String customerId = wxUser.getCustomerId();
        param.put("customerId", customerId);
        param.put("orgId", wxUser.getOrgId());
        param.put("orderSn", orderSn);
        return mallService.getOrderGoodsInfo(param);
    }


    @PostMapping("/editorderaddress")
    public void editOrderAddress(HttpServletRequest request,@RequestBody Map<String, Object> param){
        if(StringUtils.isEmpty(param.get("orderSn"))||StringUtils.isEmpty(param.get("addressId"))){
            throw GlobalException.build("参数错误");
        }
        WxUser wxUser =  requestFilter.getWxUser(request);
        String customerId = wxUser.getCustomerId();
        param.put("customerId", customerId);
        mallService.editOrderAddress(param);
    }

    /**
     * 获取订单支付状态
     * @param ： params
     */
    @PostMapping("/getPayStatus")
    public Object getPayStatus(@RequestBody Map<String, Object> param){
       return mallService.getPayStatus(param);
    }


    /**
     * 获取物流信息
     * @param shippingCode 快递单号
     * @return
     */
    @GetMapping("/getexpressinfo")
    public Map<String, Object> getExpressInfo(String shippingCode, String orderSn, HttpServletRequest request) {
        WxUser wxUser = requestFilter.getWxUser(request);
        Map<String, Object> expressList = commonsService.getExpressList(shippingCode);
        if ("0".equals(expressList.get("status"))) {
            Map data = (Map) expressList.get("result");
            data.put("infoStatus", "0");//物流状态
            return data;
        } else {
            Map<String, Object> mallDetail = mallService.getMallDetail(shippingCode,orderSn);
            Map<String, Object> data = new HashMap<>();
            data.put("infoStatus", "1");
            data.put("errorMsg", expressList.get("msg"));
            data.put("mallDetail", mallDetail);
            return data;
        }
    }


    /**
     * 获取退款售后列表
     *
     * @param ：pageIndex
     * @return
     */
    @PostMapping("/getRefundList")
    public Object getRefundList(@RequestBody Map<String, Object> params, HttpServletRequest request) {
        WxUser wxUser =  requestFilter.getWxUser(request);
        String customerId = wxUser.getCustomerId();
        params.put("customerId", customerId);
        String orgId = wxUser.getOrgId();
        params.put("orgId", orgId);
        Integer payMode = wxUser.getPayMode();
        params.put("payMode", payMode);
        return mallService.getRefundList(params);
    }


    /**
     * 获取积分商城商品列表
     * @param request
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @GetMapping("/integral/goodslist")
    public PageInfo getIntegralGoodslist(HttpServletRequest request,
                                         @RequestParam(value = "goodsType", defaultValue = "-1", required = false) int goodsType,
                                         @RequestParam(value = "pageIndex", defaultValue = "1", required = false) int pageIndex,
                                         @RequestParam(value = "pageSize", defaultValue = "10", required = false) int pageSize,Long groupId){
        WxUser wxUser = requestFilter.getWxUser(request);
        String orgId = wxUser.getOrgId();
        PageHelper.startPage(pageIndex, pageSize);
        return new PageInfo<>(mallService.getIntegralGoodslist(orgId,goodsType,groupId));
    }

    /**
     * 获取积分商城分区列表
     * @param request
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @GetMapping("/integral/grouplist")
    public List<Map> getIntegralGrouplist(HttpServletRequest request){
        WxUser wxUser = requestFilter.getWxUser(request);
        String orgId = wxUser.getOrgId();
        return mallService.getIntegralGrouplist(orgId);
    }

    /**
     * 获取积分记录
     * @param request
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @GetMapping("/integral/getscorelog")
    public Object getIntegralLog(HttpServletRequest request,
                                         @RequestParam(value = "pageIndex", defaultValue = "1", required = false) int pageIndex,
                                         @RequestParam(value = "pageSize", defaultValue = "10", required = false) int pageSize) {
        WxUser wxUser = requestFilter.getWxUser(request);
        Map<String, Object> param = new HashMap<>();
        param.put("customerId", wxUser.getCustomerId());
        param.put("orgId", wxUser.getOrgId());
        param.put("pageIndex", pageIndex);
        param.put("pageSize", pageSize);
        if (yzjOrgId.equals(wxUser.getOrgId()) && !StringUtils.isEmpty(wxUser.getSourceUserId())) {
            //雅智捷系统积分记录查询本地
            PageHelper.startPage(pageIndex, pageSize);
            return new PageInfo<>(mallService.getIntegralLogListByLocal(param));
        } else {
            return mallService.getIntegralLogList(param);
        }
    }


    /**
     * 获取积分兑换记录
     * @param request
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @GetMapping("/integral/getorderlist")
    public PageInfo getIntegralOrderList(HttpServletRequest request,
                                                 @RequestParam(value = "pageIndex", defaultValue = "1", required = false) int pageIndex,
                                                 @RequestParam(value = "pageSize", defaultValue = "10", required = false) int pageSize){
        WxUser wxUser = requestFilter.getWxUser(request);
        PageHelper.startPage(pageIndex, pageSize);
        return new PageInfo<>(mallService.getIntegralOrderList(wxUser.getCustomerId()));
    }

    @GetMapping("/integral/getOrderInfo")
    public MerchantOrderPo getIntegralOrderInfo(String orderSn, HttpServletRequest request) throws GlobalException, ParseException {
        if(StringUtils.isEmpty(orderSn)){
            throw GlobalException.build("参数错误");
        }
        WxUser wxUser =  requestFilter.getWxUser(request);
        return mallService.getIntegralOrderInfo(orderSn);
    }


    @GetMapping("/getgoodsservice")
    public List<Map<String,Object>> getGoodsService(HttpServletRequest request,Integer goodsId){
        WxUser wxUser =  requestFilter.getWxUser(request);
        return mallService.getGoodsService(goodsId);
    }

}
